package com.lpf.hotelsystem.service.impl;

import com.lpf.hotelsystem.mapper.UserMapper;
import com.lpf.hotelsystem.pojo.ActivityAnnouncement;
import com.lpf.hotelsystem.mapper.ActivityAnnouncementMapper;
import com.lpf.hotelsystem.vo.ActivityAnnouncementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

/**
 * 活动公告实体表(ActivityAnnouncement)表服务实现类
 *
 * @author makejava
 * @since 2025-01-18 17:13:02
 */
@Service("activityAnnouncementService")
public class ActivityAnnouncementServiceImpl {
    @Resource
    private ActivityAnnouncementMapper activityAnnouncementMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param promotionId 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer promotionId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", this.activityAnnouncementMapper.selectById(promotionId));
        map.put("msg", "查询单条记录成功");
        return map;
    }

    /**
     * 一次性全部查询
     *
     * @return 查询结果
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "全部查询成功");
        map.put("data", this.activityAnnouncementMapper.selectAll());
        return map;
    }

    /**
     * 分页查询
     *
     * @param activityAnnouncement 筛选条件
     * @param page        要查询第几页
     * @param limit       每页显示多少条
     * @return 查询结果
     */
    public Map<String, Object> selectByLimit(ActivityAnnouncement activityAnnouncement, int page, int limit) {
        // page 页码   (page-1)*limit=起始下标index
        int startIndex = (page - 1) * limit;   // 计算出起始下标
        List<ActivityAnnouncement> list = this.activityAnnouncementMapper.selectByLimit(activityAnnouncement, startIndex, limit);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        
        // 获取满足条件的总条数
        long count = this.activityAnnouncementMapper.selectCount(activityAnnouncement);
        // 计算总页数
        long maxPage = (count - 1) / limit + 1;
        
        map.put("count", count);
        map.put("maxPage", maxPage);
        map.put("nowPage", page);
        map.put("data", list);
        return map;
    }
    
    /**
     * 新增数据
     *
     * @param activityAnnouncement 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert( ActivityAnnouncement activityAnnouncement) {
        ActivityAnnouncement activityAnnouncement01 = new ActivityAnnouncement();//把前端传递的值对象数据，封装到pojo对象中去(反射机制)
        BeanUtils.copyProperties(activityAnnouncement , activityAnnouncement01 );
        activityAnnouncement.setPubDate(new Date());
        activityAnnouncementMapper.insert(activityAnnouncement);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增成功");
        map.put("data", activityAnnouncement01);
        return map;
    }

    /**
     * 修改数据
     *
     * @param activityAnnouncement 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(ActivityAnnouncement activityAnnouncement) {
        this.activityAnnouncementMapper.updateById(activityAnnouncement);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", activityAnnouncement);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param promotionId 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(Integer promotionId) {
        Map<String, Object> map = new HashMap<>();
        activityAnnouncementMapper.deleteById(promotionId);
        map.put("code", 200);
//        map.put("id", promotionId);
//        map.put("msg", this.activityAnnouncementMapper.deleteById(promotionId) > 0 ? "删除成功" : "删除失败");
        map.put("msg","删除用户成功");
        return map;
    }
}
